#include     "gpio.h"
#include     "main.h"
#include     <math.h>
#include     <stdio.h>      /*标准输入输出定义*/
#include     <stdlib.h>     /*标准函数库定义*/
#include     <unistd.h>     /*Unix 标准函数定义*/
#include     <sys/types.h>
#include     <sys/stat.h>
#include     <fcntl.h>      /*文件控制定义*/
#include     <termios.h>    /*PPSIX 终端控制定义*/
#include     <errno.h>      /*错误号定义*/

#define trUtf8 qApp->trUtf8

void read_config(QString strTemp) //strTemp 读文件名
  {
      QFile fileRead(strTemp);
      fileRead.open(QIODevice::ReadOnly);
      QDataStream  readDataStream(&fileRead);
      readDataStream >> whole_machine_parameter
                     >>  textiles_datamap
                     >>  metals_datamap
                     >>  bottom_datamap
                     >>  liquid_datamap
                     >>  user_define1_datamap
                     >>  user_define2_datamap
                     >>  user_define3_datamap
                     >>  sensor_adjust_datamap
                     >>  IO_setting_datamap;
      fileRead.close();


    }

void save_config(QString strTemp)//strTemp写入文件名
{
    if(config_changed)
    {
         QFile fileWrite(strTemp);
         fileWrite.open(QIODevice::WriteOnly);
         QDataStream  writeDataStream(&fileWrite);
         writeDataStream<< whole_machine_parameter
                       <<  textiles_datamap
                       <<  metals_datamap
                       <<  bottom_datamap
                       <<  liquid_datamap
                       <<  user_define1_datamap
                       <<  user_define2_datamap
                       <<  user_define3_datamap
                       <<  sensor_adjust_datamap
                       <<  IO_setting_datamap;

         config_changed=false;
         fileWrite.close();
    }

}

void certificate()
{
    bool password_right=0;
    Password_input b;
    b.ui->esc->setDisabled(true);

    if(whole_machine_parameter.used_times_counter>1000) {
        if(whole_machine_parameter.license<3)  {
            b.ui->lineEdit->setText(trUtf8("系统故障0x003"));
            b.setpassword("81415987");
            b.exec();
            whole_machine_parameter.license=3;
            password_right=1;
        }}
    else if(whole_machine_parameter.used_times_counter>500)
      {
        if(whole_machine_parameter.license<2)
          {
            b.ui->lineEdit->setText(trUtf8("系统故障0x002"));
            b.setpasswordgoup("*","81415132","81415987");
            b.exec();
            if(b.whichpassword==2) whole_machine_parameter.license=2;
            else if(b.whichpassword==3) whole_machine_parameter.license=3;
             password_right=1;
           }
       }
    else if(whole_machine_parameter.used_times_counter>200)
    {
        if(whole_machine_parameter.license<1)  {
            b.ui->lineEdit->setText(trUtf8("系统故障0x001"));
            b.setpasswordgoup("81414923","81415132","81415987");
            b.exec();
            switch(b.whichpassword)
            {
           case 1: whole_machine_parameter.license=1;break;
           case 2: whole_machine_parameter.license=2;break;
           case 3: whole_machine_parameter.license=3;break;
           default:break;
            }
            password_right=1;
        }}

    if( password_right)
    {
        config_changed=true;
        ProgressDialog p;
        p.start(50);
        p.exec();
           }

}

#define SETUP_GPIOMAP(GPIOCON,GPIOCON_offset,GPIODAT,GPIODAT_offset)\
    GPIOCON=(volatile unsigned int*)(GPA_base+GPIOCON_offset);\
    GPIODAT =  (volatile unsigned int*)(GPA_base+GPIODAT_offset);
 //   GPIOPUD =  (volatile unsigned int*)(GPA_base+GPIOPUD_offset);

#define SETUP_GPIOMAP2(GPIOCON0,GPIOCON0_offset,GPIOCON1,GPIOCON1_offset,GPIODAT,GPIODAT_offset)\
    GPIOCON0=(volatile unsigned int*)(GPA_base+GPIOCON0_offset);\
    GPIOCON1=(volatile unsigned int*)(GPA_base+GPIOCON1_offset);\
    GPIODAT =  (volatile unsigned int*)(GPA_base+GPIODAT_offset);


void GPIO_SETUP()
{

    unsigned char* GPA_base;
    int fd;

    if((fd=open("/dev/mem",O_RDWR|O_SYNC))==-1){printf("open /dev/mem fail!\n");  }
    GPA_base=(unsigned char*) mmap(0,0x1000,PROT_READ|PROT_WRITE,MAP_SHARED,fd,GPA_addr);

    SETUP_GPIOMAP(GPCCON,GPCCON_offset,GPCDAT,GPCDAT_offset)
    SETUP_GPIOMAP(GPMCON,GPMCON_offset,GPMDAT,GPMDAT_offset)
    SETUP_GPIOMAP(GPNCON,GPNCON_offset,GPNDAT,GPNDAT_offset)
    SETUP_GPIOMAP(GPPCON,GPPCON_offset,GPPDAT,GPPDAT_offset)
    SETUP_GPIOMAP(GPQCON,GPQCON_offset,GPQDAT,GPQDAT_offset)
    SETUP_GPIOMAP(GPECON,GPECON_offset,GPEDAT,GPEDAT_offset)
    SETUP_GPIOMAP(GPFCON,GPFCON_offset,GPFDAT,GPFDAT_offset)
    SETUP_GPIOMAP(GPOCON,GPOCON_offset,GPODAT,GPODAT_offset)
    SETUP_GPIOMAP2(GPKCON0,GPKCON0_offset,GPKCON1,GPKCON1_offset,GPKDAT,GPKDAT_offset)
    SETUP_GPIOMAP2(GPLCON0,GPLCON0_offset,GPLCON1,GPLCON1_offset,GPLDAT,GPLDAT_offset)


    *GPECON  = 0x11111;
    *GPFCON  = 0xA5555555;
    *GPCCON  = 0xffff0000;

    *GPQCON  = 0xFC157; //1111 1100 0001 0101 0111
    *GPMCON  = 0x111111;
    *GPKCON0&= 0x1111ffff;
    *GPKCON1 = 0xff11fff1;
    *GPLCON1 = 0xfffff100;
    *GPNCON &= 0xff33cfff;

//未初始化不能使用
//    water_valve_OFF;
//    heating_valve_OFF;
//    Vacuum_pump_OFF;
//    air_intake_valve_OFF;
//    exhaust_valve_OFF;
//    air_supply_valve_OFF;
    SET_GPIO_OFF(GPEDAT,1)
    SET_GPIO_OFF(GPEDAT,2)
    SET_GPIO_OFF(GPEDAT,3)
//    SET_GPIO_OFF(GPEDAT,4)
    SET_GPIO_OFF(GPMDAT,0)
    SET_GPIO_OFF(GPMDAT,1)
    LEDA_ON
    LED2_OFF;
    LED3_OFF;
    LED4_OFF;
//    IO_drive_type=0;

}

void COM_SETUP(int &fd,const char* tty)
 {
         fd = ::open( tty, O_RDWR);
         if (-1 == fd){        
             ::close(fd);
         perror(" com open failed !");
         WARN->show_text("串口打开失败！");
         }
         struct termios Opt;
         tcgetattr(fd, &Opt);
         tcflush(fd,TCIFLUSH);
         cfsetispeed(&Opt,B9600);
         cfsetospeed(&Opt,B9600);
         Opt.c_cflag |= CS8;                          //设置数据位
         Opt.c_cflag &= ~PARENB;                      //无校验
         Opt.c_cflag &= ~CSTOPB;                      //1waim位停止位
         Opt.c_oflag &= ~(OPOST);                     //原始数据输出

         Opt.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);//ECHOE
         Opt.c_iflag &= ~(INPCK|BRKINT|ICRNL|ISTRIP|IXON);
         Opt.c_cc[VMIN] = 0;                       //读取字符的最小数量
         Opt.c_cc[VTIME] = 10;                   //读取第一个字符的等待时间

        if( tcsetattr(fd,TCSANOW, &Opt)!=0)        //配置生效
     {  perror("com set error!");   }

 }

 int sensor_test()
 {

     int refrence_pressure=pow(10,7.07406-1657.46/(sterilization_temperature+0.5+227.2))-100;
     if(PSU>refrence_pressure+15 || PSU<refrence_pressure-5)
     { WARN->show_text("传感器不对应，请校准！");
         return -1;}
     else return 0;

 }

 void file_clean(const char* dirpath)
 {

    QDir dir(dirpath);
    QStringList filelist=dir.entryList();
    int size=filelist.size();
    if(size>100)
    {
        dir.setSorting(QDir::Time);
        QFile file;
        for(int i=50;i<size;i++)
        {
          file.setFileName(dirpath +filelist.at(i));
          file.remove();
        }

     }
 }


 void sterilization_default_setting_recovery(QMap<QString,int >& datamap,uint checktype)
 {


     switch(checktype)
  {
     case 1:
     {
     datamap[trUtf8("灭菌温度")]=134;
     datamap[trUtf8("灭菌时间")]=360;
     datamap[trUtf8("预平衡时间")]=180;
     datamap[trUtf8("脉动次数")]=3;
     datamap[trUtf8("脉动压力下限")]=-70;
     datamap[trUtf8("脉动压力上限")]=100;
     datamap[trUtf8("干燥压力")]=-70;
     datamap[trUtf8("干燥时间")]=180;
     datamap[trUtf8("干燥次数")]=2;
     datamap[trUtf8("加热延时")]=30;

     }
         break;
     case 2:

     {
         datamap[trUtf8("灭菌温度")]=126;
         datamap[trUtf8("灭菌时间")]=1800;
         datamap[trUtf8("预平衡时间")]=180;
         datamap[trUtf8("排冷次数")]=3;
         datamap[trUtf8("排冷压力")]=100;
         datamap[trUtf8("加热延时")]=30;

     }
      break;
     case 3:
         datamap[trUtf8("恒温上限")]=9;
         datamap[trUtf8("恒温下限")]=6;
         datamap[trUtf8("下排式胶条抽气时间")]=90;
         datamap[trUtf8("真空式胶条抽气时间")]=20;
         break;
     case 4:
         datamap[trUtf8("加水")]=1;
         datamap[trUtf8("加热")]=2;
         datamap[trUtf8("进气")]=3;
         datamap[trUtf8("排气")]=4;
         datamap[trUtf8("真空泵")]=5;
         datamap[trUtf8("回气")]=6;
         datamap[trUtf8("开前门")]=7;
         datamap[trUtf8("关前门")]=8;
         datamap[trUtf8("前密封")]=9;
         datamap[trUtf8("前解封")]=10;
         datamap[trUtf8("开后门")]=11;
         datamap[trUtf8("关后门")]=12;
         datamap[trUtf8("后密封")]=13;
         datamap[trUtf8("后解封")]=14;
         break;
     default:break;
  }
     config_changed=true;
}
